<template>
  <div class="userRegisterPage">
    <h2 class="userLoginPage-title">鸡哥AI应用生成-用户注册</h2>
    <h3 class="userLoginPage-desc">不写一行代码，生成完整应用</h3>
    <a-form :model="formState" autocomplete="off" name="normal_register" @finish="handleSubmit">
      <a-form-item
        :rules="[{ required: true, message: '请输入账号' }]"
        label="用户名"
        name="userAccount"
      >
        <a-input v-model:value="formState.userAccount" placeholder="请输入用户名">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能小于8' },
        ]"
        label="密码"
        name="userPassword"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        :rules="[{ required: true, message: '请确认密码' }, { validator: validateConfirmPassword }]"
        label="确认密码"
        name="checkPassword"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="请再次输入密码">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <div class="tips">
        已有账号？
        <RouterLink to="/user/login">去登录</RouterLink>
      </div>

      <a-form-item>
        <a-button
          class="register-form-button"
          html-type="submit"
          style="width: 100%"
          type="primary"
        >
          注册
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { LockOutlined, UserOutlined } from '@ant-design/icons-vue'
import { userRegister } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import router from '@/router'

interface FormState {
  userAccount: string
  userPassword: string
  checkPassword: string
}

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

// 自定义密码确认验证
const validateConfirmPassword = async (_rule: any, value: string) => {
  if (value && value !== formState.userPassword) {
    return Promise.reject(new Error('两次输入的密码不一致'))
  }
  return Promise.resolve()
}

const handleSubmit = async (values: any) => {
  try {
    const res = await userRegister(values)
    if (res.data.code === 0) {
      message.success('注册成功，请登录')
      // 注册成功后跳转到登录页面
      await router.push({
        path: '/user/login',
        replace: true,
      })
    } else {
      message.error('注册失败：' + res.data.message)
    }
  } catch (error) {
    message.error('注册失败，请重试')
    console.error('Registration error:', error)
  }
}
</script>

<style scoped>
.userRegisterPage {
  width: 360px;
  margin: 300px auto;
  height: 600px;
}

.tips {
  text-align: right;
  margin-bottom: 16px;
}

.userLoginPage-title {
  text-align: center;
}

.userLoginPage-desc {
  text-align: center;
  font-weight: lighter;
  margin-bottom: 32px;
}
</style>
